<template>
	<view>
		<view class="wrap">
			<view class="u-search-box">
				<u-search placeholder="输入商品搜索" class="u-search-text" v-model="keyword" @custom="searchGoods" @clear="clearSearch"></u-search>
			</view>
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(item, index) in orderList[1]" :key="item.id">
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">单号：{{ item.order_no }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view class="right">{{ status[item.status-1] }}</view>
								</view>
								<view class="item"  v-for="(goods, gindex) in item.goods.data" :key="gindex">
									<view class="left"><image :src="goods.cover_url" mode="aspectFill"></image></view>
									<view class="content">
										<view class="title u-line-2">{{ goods.title }}</view>
										<view class="type">{{ goods.description }}</view>
										<view class="delivery-time">订单：{{ goods.updated_at }}</view>
									</view>
									
										<view class="right">
										<view class="price">
											￥{{ priceInt(item.orderDetails.data[gindex].price) }}
											<text class="decimal">.{{ priceDecimal(item.orderDetails.data[gindex].price) }}</text>
										</view>
										<view class="number">x{{ item.orderDetails.data[gindex].num }}</view>
									</view>
				
									
								</view>
								<view class="total">
									共{{ totalNum(item.orderDetails.data) }}件商品 合计:
									<text class="total-price">
										<!-- ￥{{ priceInt(totalPrice(item.orderDetails.data)) }}.
										<text class="decimal">{{ priceDecimal(totalPrice(item.orderDetails.data)) }}</text> -->
										{{item.amount}}
									</text>
								</view>
								<view class="bottom">
									<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon></view>
									<view class="logistics btn">查看物流</view>
									<view class="exchange btn">卖了换钱</view>
									<view class="evaluate btn">评价</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(item, index) in orderList[2]" :key="item.id">
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">单号：{{ item.order_no }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view class="right">{{ status[item.status-1] }}</view>
								</view>
								<view class="item"  v-for="(goods, gindex) in item.goods.data" :key="gindex">
									<view class="left"><image :src="goods.cover_url" mode="aspectFill"></image></view>
									<view class="content">
										<view class="title u-line-2">{{ goods.title }}</view>
										<view class="type">{{ goods.description }}</view>
										<view class="delivery-time">订单：{{ goods.updated_at }}</view>
									</view>
									
										<view class="right">
										<view class="price">
											￥{{ priceInt(item.orderDetails.data[gindex].price) }}
											<text class="decimal">.{{ priceDecimal(item.orderDetails.data[gindex].price) }}</text>
										</view>
										<view class="number">x{{ item.orderDetails.data[gindex].num }}</view>
									</view>
				
									
								</view>
								<view class="total">
									共{{ totalNum(item.orderDetails.data) }}件商品 合计:
									<text class="total-price">
										<!-- ￥{{ priceInt(totalPrice(item.orderDetails.data)) }}.
										<text class="decimal">{{ priceDecimal(totalPrice(item.orderDetails.data)) }}</text> -->
										{{item.amount}}
									</text>
								</view>
								<view class="bottom">
									<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon></view>
									<view class="logistics btn">查看物流</view>
									<view class="exchange btn">卖了换钱</view>
									<view class="evaluate btn">评价</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(item, index) in orderList[3]" :key="item.id">
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">单号：{{ item.order_no }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view class="right">{{ status[item.status-1] }}</view>
								</view>
								<view class="item"  v-for="(goods, gindex) in item.goods.data" :key="gindex">
									<view class="left"><image :src="goods.cover_url" mode="aspectFill"></image></view>
									<view class="content">
										<view class="title u-line-2">{{ goods.title }}</view>
										<view class="type">{{ goods.description }}</view>
										<view class="delivery-time">订单：{{ goods.updated_at }}</view>
									</view>
									
										<view class="right">
										<view class="price">
											￥{{ priceInt(item.orderDetails.data[gindex].price) }}
											<text class="decimal">.{{ priceDecimal(item.orderDetails.data[gindex].price) }}</text>
										</view>
										<view class="number">x{{ item.orderDetails.data[gindex].num }}</view>
									</view>
				
									
								</view>
								<view class="total">
									共{{ totalNum(item.orderDetails.data) }}件商品 合计:
									<text class="total-price">
										<!-- ￥{{ priceInt(totalPrice(item.orderDetails.data)) }}.
										<text class="decimal">{{ priceDecimal(totalPrice(item.orderDetails.data)) }}</text> -->
										{{item.amount}}
									</text>
								</view>
								<view class="bottom">
									<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon></view>
									<view class="logistics btn">查看物流</view>
									<view class="exchange btn">卖了换钱</view>
									<view class="evaluate btn">评价</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(item, index) in orderList[4]" :key="item.id">
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">单号：{{ item.order_no }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view class="right">{{ status[item.status-1] }}</view>
								</view>
								<view class="item"  v-for="(goods, gindex) in item.goods.data" :key="gindex">
									<view class="left"><image :src="goods.cover_url" mode="aspectFill"></image></view>
									<view class="content">
										<view class="title u-line-2">{{ goods.title }}</view>
										<view class="type">{{ goods.description }}</view>
										<view class="delivery-time">订单：{{ goods.updated_at }}</view>
									</view>
									
										<view class="right">
										<view class="price">
											￥{{ priceInt(item.orderDetails.data[gindex].price) }}
											<text class="decimal">.{{ priceDecimal(item.orderDetails.data[gindex].price) }}</text>
										</view>
										<view class="number">x{{ item.orderDetails.data[gindex].num }}</view>
									</view>
				
									
								</view>
								<view class="total">
									共{{ totalNum(item.orderDetails.data) }}件商品 合计:
									<text class="total-price">
										<!-- ￥{{ priceInt(totalPrice(item.orderDetails.data)) }}.
										<text class="decimal">{{ priceDecimal(totalPrice(item.orderDetails.data)) }}</text> -->
										{{item.amount}}
									</text>
								</view>
								<view class="bottom">
									<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon></view>
									<view class="logistics btn">查看物流</view>
									<view class="exchange btn">卖了换钱</view>
									<view class="evaluate btn">评价</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(item, index) in orderList[5]" :key="item.id">
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">单号：{{ item.order_no }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view class="right">{{ status[item.status-1] }}</view>
								</view>
								<view class="item"  v-for="(goods, gindex) in item.goods.data" :key="gindex">
									<view class="left"><image :src="goods.cover_url" mode="aspectFill"></image></view>
									<view class="content">
										<view class="title u-line-2">{{ goods.title }}</view>
										<view class="type">{{ goods.description }}</view>
										<view class="delivery-time">订单：{{ goods.updated_at }}</view>
									</view>
									
										<view class="right">
										<view class="price">
											￥{{ priceInt(item.orderDetails.data[gindex].price) }}
											<text class="decimal">.{{ priceDecimal(item.orderDetails.data[gindex].price) }}</text>
										</view>
										<view class="number">x{{ item.orderDetails.data[gindex].num }}</view>
									</view>
				
									
								</view>
								<view class="total">
									共{{ totalNum(item.orderDetails.data) }}件商品 合计:
									<text class="total-price">
										<!-- ￥{{ priceInt(totalPrice(item.orderDetails.data)) }}.
										<text class="decimal">{{ priceDecimal(totalPrice(item.orderDetails.data)) }}</text> -->
										{{item.amount}}
									</text>
								</view>
								<view class="bottom">
									<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon></view>
									<view class="logistics btn">查看物流</view>
									<view class="exchange btn">卖了换钱</view>
									<view class="evaluate btn">评价</view>
								</view>
							</view>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			orderList: [[], [], [], [], []],
			list: [
				{
					name: '待付款'
				},
				{
					name: '已支付'
				},
				{
					name: '待收货'
				},
				{
					name: '已完成',
					count: 12
				},
				{
					name: '已过期',
					count: 12
				}
			],
			status:[
				'未支付','已付款','已发货','已完成',' 已过期'  //  去付款 ， '' ,  确认收货 ， 立即评价 ， ’‘
			], //订单状态
			current: 0,
			swiperCurrent: 0,
			tabsHeight: 0,
			dx: 0,
			loadStatus: ['loadmore','loadmore','loadmore','loadmore','loadmore'],
			page: 1, //分页
			keyword: '', //搜索关键字
			isLast: false //是否是最后一页
		};
	},
	onLoad() {
		this.getOrderList(1);
		this.getOrderList(2);
		this.getOrderList(3);
		this.getOrderList(4);
		this.getOrderList(5);
	},
	computed: {
		// 价格小数
		priceDecimal() {
			return val => {
				if(this.$u.test.amount(val) ){
					if (val !== parseInt(val)) return val.slice(-2);
				    else return '00';
				}
				return false;
			};
		},
		// 价格整数
		priceInt() {
			return val => {
				if(this.$u.test.amount(val) ){
					if (val !== parseInt(val)) return val.split('.')[0];
				    else return val;
				}else{
					return val;
				}
				
				
			};
		}
	},
	methods: {
		reachBottom() {
			// 此tab为空数据
			if(this.current != 2) {
				this.loadStatus.splice(this.current,1,"loading")
				setTimeout(() => {
					this.getOrderList(this.current);
				}, 1200);
			}
		},
		// 页面数据
		async getOrderList(idx) {
			const param = {
				page: this.page,
				title: this.keyword,
				status:idx,
				include:'goods,user,orderDetails'
			}
			const res = await this.$u.api.orderList(param);
			this.orderList[idx] = res.data;
			this.loadStatus.splice(this.current,1,"loadmore")
			// console.log(this.orderList[idx]);
		
		},
		/**
		 * 搜索商品
		 */
		searchGoods() {
			// 搜索商品，进行重置分类ID
			this.current = 0;
			this.initData();
		},
		/**
		 * 清除搜索商品
		 */
		clearSearch() {
			this.keyword = '';
			this.initData();
		},
		/**
		 * 重置商品和分页
		 */
		initData() {
			this.page = 1;
			orderList: [[], [], [], [], []],
			this.getOrderList(1);
			this.getOrderList(2);
			this.getOrderList(3);
			this.getOrderList(4);
			this.getOrderList(5);
		},
		// 页面滚动到底部的事件
		onReachBottom(e) {
			console.log(e);
			// // 如果是最后一页，则返回
			// if(this.isLast) return ;
			// // 重新请求数据，带上分页参数
			// this.page = this.page + 1 ;
			// this.getData();
		},
		// 总价
		totalPrice(item) {
			let price = 0;
			item.map(val => {
				price += parseFloat(val.price*val.num);
			});
			return price.toFixed(2);
		},
		// 总件数
		totalNum(item) {
			let num = 0;
			item.map(val => {
				num += val.num;
			});
			return num;
		},
		// tab栏切换
		change(index) {
			this.swiperCurrent = index;
			this.getOrderList(index);
		},
		transition({ detail: { dx } }) {
			this.$refs.tabs.setDx(dx);
		},
		animationfinish({ detail: { current } }) {
			this.$refs.tabs.setFinishCurrent(current);
			this.swiperCurrent = current;
			this.current = current;
		}
	}
};
</script>

<style>
/* #ifndef H5 */
page {
	height: 100%;
	background-color: #f2f2f2;
}
/* #endif */
</style>

<style lang="scss" scoped>
	.u-search-box {
		padding: 18rpx 30rpx;
		background-color: #FFFFFF;
	}

.order {
	width: 710rpx;
	background-color: #ffffff;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;
	.top {
		display: flex;
		justify-content: space-between;
		.left {
			display: flex;
			align-items: center;
			.store {
				margin: 0 10rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		.right {
			color: $u-type-warning-dark;
		}
	}
	.item {
		display: flex;
		margin: 20rpx 0 0;
		.left {
			margin-right: 20rpx;
			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}
		.content {
			.title {
				font-size: 28rpx;
				line-height: 50rpx;
			}
			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}
			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}
		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: right;
			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}
			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}
	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
		}
	}
	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;
		.btn {
			line-height: 52rpx;
			width: 160rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 26rpx;
			text-align: center;
			color: $u-type-info-dark;
		}
		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
		}
	}
}
.centre {
	text-align: center;
	margin: 200rpx auto;
	font-size: 32rpx;
	image {
		width: 164rpx;
		height: 164rpx;
		border-radius: 50%;
		margin-bottom: 20rpx;
	}
	.tips {
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}
	.btn {
		margin: 80rpx auto;
		width: 200rpx;
		border-radius: 32rpx;
		line-height: 64rpx;
		color: #ffffff;
		font-size: 26rpx;
		background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
	}
}
.wrap {
	display: flex;
	flex-direction: column;
	height: calc(100vh - var(--window-top));
	width: 100%;
}
.swiper-box {
	flex: 1;
}
.swiper-item {
	height: 100%;
}
</style>
